<template>
  <div class="header">
    <div class="left">
      <el-icon class="back" v-if="state.hasBack" @click="back">
        <Back />
      </el-icon>
      <span style="font-size: 20px">{{ state.name }}</span>
    </div>
    <div class="right">
      <div class="right-btns" @click="fullScreen"><el-icon :size="18" class="right-btn">
          <FullScreen />
        </el-icon></div>
      <el-popover placement="bottom" :width="320" trigger="click" popper-class="popper-user-box">
        <template #reference>
          <div class="author">
            <!-- <i class="icon el-icon-s-custom" /> -->
            <div>
              <el-avatar :icon="UserFilled" :size="32" class="u-ava" />
            </div>
            <div class="u-name">
              {{ state.userInfo && state.userInfo.nickName || '' }}
            </div>
            <i class="el-icon-caret-bottom" />
          </div>
        </template>
        <div class="nickname">
          <p>登录名：{{ state.userInfo && state.userInfo.loginUserName || '' }}</p>
          <p>昵称：{{ state.userInfo && state.userInfo.nickName || '' }}</p>
          <el-tag size="small" effect="dark" class="logout" @click="logout">退出</el-tag>
        </div>
      </el-popover>
    </div>
  </div>
</template>

<script setup>
import { UserFilled, FullScreen } from '@element-plus/icons-vue'
import { onMounted, reactive } from 'vue'
import { useRouter } from 'vue-router'
import axios from '@/utils/axios'
import { localRemove, pathMap } from '@/utils'

const router = useRouter()
const state = reactive({
  name: '',
  userInfo: null, // 用户信息变量
  hasBack: false, // 是否展示返回icon
})
//进入和退出全屏
const fullScreen = () => {

  let element = document.documentElement;
  if (element.requestFullscreen) {
    element.requestFullscreen();
  } else if (element.webkitRequestFullScreen) {
    element.webkitRequestFullScreen();
  } else if (element.mozRequestFullScreen) {
    element.mozRequestFullScreen();
  } else if (element.msRequestFullscreen) {
    // IE11
    element.msRequestFullscreen();
  }
  //退出全屏
  if (document.webkitCancelFullScreen) {
    document.webkitCancelFullScreen();
  } else if (document.mozCancelFullScreen) {
    document.mozCancelFullScreen();
  } else if (document.msExitFullscreen) {
    // IE11
    document.msExitFullscreen();
  }

}
// 初始化执行方法
onMounted(() => {
  const pathname = router.currentRoute.value.path
  if (!['/login'].includes(pathname)) {
    getUserInfo()
  }
})
// 获取用户信息
const getUserInfo = async () => {
  const userInfo = await axios.get('/adminUser/profile')
  state.userInfo = userInfo
}
// 退出登录
const logout = () => {
  // 回到登录页
  window.location.href = '/login'
  axios.delete('/logout').then(() => {
    // 退出之后，将本地保存的 token  清理掉
    localRemove('token')
  })
}

router.afterEach((to) => {
  // const { id } = to.query
  state.name = pathMap[to.name]
  // level2 和 level3 需要展示返回icon
  // console.log('to.name', to.name)
  state.hasBack = ['level2', 'level3'].includes(to.name)
})

// 返回方法
const back = () => {
  router.back()
}
</script>

<style scoped>
.header {
  height: 50px;
  border-bottom: 1px solid #e9e9e9;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}

.header .left .back {
  border: 1px solid #e9e9e9;
  padding: 5px;
  border-radius: 50%;
  margin-right: 5px;
  cursor: pointer;
}

.right {
  display: flex;
  align-items: center;
  height: 100%;
}

.right>div>.icon {
  font-size: 18px;
  margin-right: 6px;
}

.right-btns {
  display: flex;
  align-items: center;
  height: inherit;
}

.right-btn {
  cursor: pointer;
  padding: 5px;
  margin: 5px 0;
  height: inherit;
}

.right-btn:hover {
  background: #f5f5f5;
}

.author {
  display: flex;
  align-items: center;
  margin-left: 10px;
  cursor: pointer;

}

.u-ava {
  margin-right: 5px;
  background: #166aaf;
}

.u-name {
  color: #4b4b4b;
}
</style>

<style>
.popper-user-box {
  background: linear-gradient(116deg, #0f0c29, #302b63, #24243e) !important;
  /* background-size: cover !important; */
  border-radius: 5px !important;
}

.popper-user-box .nickname {
  position: relative;
  color: #ffffff;
}

.popper-user-box .nickname .logout {
  position: absolute;
  right: 0;
  top: 0;
  cursor: pointer;
}
</style>